import { Message } from '@alifd/next';
import configService from '@/services/config';

export default {
  state: {
    list: [],
  },
  reducers: {
    update: (prevState, payload) => {
      return { ...prevState, ...payload };
    },
  },
  effects: (dispatch) => ({
    getList: async (params) => {
      const data = await configService.list(params);
      if (data.code === 0) {
        dispatch.config.update({ list: data.page });
      } else {
        Message.error(data.msg);
      }
    },
    postCreate: async (data) => {
      const res = await configService.add(data);
      if (res.code === 0) {
        Message.success({
          content: '新增成功！',
          hasMask: true,
        });
      } else {
        Message.error({
          content: res.msg,
          hasMask: true,
        });
      }
    },
    postUpdate: async (data) => {
      const res = await configService.update(data);
      if (res.code === 0) {
        Message.success({
          content: '编辑成功！',
          hasMask: true,
        });
      } else {
        Message.error({
          content: res.msg,
          hasMask: true,
        });
      }
    },
    postDelete: async (ids) => {
      const res = await configService.delete(ids);
      if (res.code === 0) {
        Message.success({
          content: '删除成功！',
          hasMask: true,
        });
      } else {
        Message.error({
          content: res.msg,
          hasMask: true,
        });
      }
    },
  }),
};
